<?php
class game_slider_module extends CI_Module {
	public function init() {
		$this->load->model("mgame");
		$this->load->model("mcategory");
	}
	
	public function active() {
		
	}
	
	public function deactive() {
		
	}
	
	public function admin($config) {
	
		//$title = $config['title'];
		//$wconfig = $config['config'];
		//$layout = $config['layout'];
		
		//$config['config']['list_cate'] = $this->mcategory->get_cat_tree();
		//$config['config']['list_cate'][0] ='Tất cả';
		$this->view('admin', $config['config']);
	}
	
	/**
	 * 
	 * @param object $config
	 * 
	 */
	public function run($widget_config) {
		$config = $widget_config;
		//$config = @$config['config'];
		//$layout = @$config['layout'];
		$view = (isset($config['view'] ) && $config['view']) ? $config['view'] : 'home';
		$type = (isset($config['type'] ) && $config['type']) ? $config['type'] : 'newgame';
		$show_current_category = (isset($config['show_current_category'] ) && $config['show_current_category'] =='true') ? true : false;
		$game_id_hot = explode(",",@$config['game_id_hot']);
		$game_id_slider = explode(",",@$config['game_id_slider']);
		$game_id_not_in = array();
		
		switch ($type) {
			case 'newgame':
				$config['games_slider'] = $this->gets_game($game_id_slider,$show_current_category,'new_game');
				foreach($config['games_slider'] as $g) $game_id_not_in[] = $g->game_id;
				$config['games_hot'] = $this->gets_game( $game_id_hot ,$show_current_category,'new_game',$game_id_not_in);
			break;
			case 'topplay':
				 $config['games_slider'] = $this->gets_game($game_id_slider,$show_current_category,'topplay');
				foreach($config['games_slider'] as $g) $game_id_not_in[] = $g->game_id;
				$config['games_hot'] = $this->gets_game($game_id_hot,$show_current_category,'topplay',$game_id_not_in);
			break;
		}
		$this->view('home', $config);
	}
	
	function gets_game($arr_game_id = array(),$show_current_category = false, $type = 'new_game', $arr_game_id_not_in = array()){
		$this->db->_protect_identifiers = FALSE;
		$this->db->order_by(" FIELD(game_id,".implode(",",$arr_game_id).")");
		$this->db->where('game_status >',0);
		$games =  $this->mgame->where_in($arr_game_id,'game_id');
		$this->db->_protect_identifiers = TRUE;

		$num = count($arr_game_id) - count($games);
		$ngames = $games;
		if($num >0){
			if($show_current_category && $this->msystem->get_global('cat_id')){
				$this->db->select("cat_id");
				$this->db->where('cat_status',1);
				
				//lấy danh sách các danh mục cấp 1
				$subs_cat = $this->mcategory->gets(array_shift($this->msystem->get_global('cat_id')),'cat_parent');
				//lấy danh sách các danh mục cấp 2
				if(!$subs_cat){
					$subs_cat = $this->mcategory->gets(array_shift($this->msystem->get_global('cat_id')),'cat_id');
				}
				if($subs_cat){
					$cat = array();
					foreach($subs_cat as $sub_cat)
					{
						$cat[] = $sub_cat->cat_id;
					}
					$this->db->join("game_category as gc","gc.game_id = game.game_id");
					$this->db->where_in("gc.cat_id",$cat);
				}
				
			}
			$this->db->limit($num);
			if($type =='new_game')
				$this->db->order_by('game.game_id desc');
			else
				$this->db->order_by('game.game_view desc');
			
			if(count($arr_game_id_not_in) >0){
				$this->db->where_not_in('game.game_id',$arr_game_id_not_in);
			}
			$this->db->group_by('game.game_id');
			$this->db->where('game.game_status >', 0);
			$games_new = $this->mgame->gets(0,'game_status >');
			 
			for($i=0; $i<count($arr_game_id); $i++){
				if($arr_game_id[$i] ==0 || count($games) ==0){
					$ggname = array_shift($games_new);
				}
				else {
					$ggname = array_shift($games);
				}
				if($ggname)
					$ngames[] = $ggname;
			}
			
		}
		return $ngames;
	}
	
}